package com.zlkj.pro.modular.business.service.impl;

import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlkj.pro.core.util.ConvertUtil;
import com.zlkj.pro.modular.business.entity.ProductInfoEntity;
import com.zlkj.pro.modular.business.mapper.ProductInfoMapper;
import com.zlkj.pro.modular.business.model.dto.ProductInfoDTO;
import com.zlkj.pro.modular.business.model.vo.ProductInfoVO;
import com.zlkj.pro.modular.business.service.ProductInfoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author liyang
 * @date 2024/4/6
 * @time 14:34
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfoEntity> implements ProductInfoService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private Environment env;

    @Override
    public PageResult<ProductInfoVO> findPage(ProductInfoDTO dto) {
        Integer pageNo = dto.getPageNo();
        Integer pageSize = dto.getPageSize();
        List<ProductInfoVO> productInfoAdminList = productInfoMapper.findPage(dto);
        if (CollectionUtils.isNotEmpty(productInfoAdminList)) {
            productInfoAdminList.forEach(productInfoVO -> {
                productInfoVO.setImageDetailFileUrl(env.getProperty("file-path") + productInfoVO.getImageDetailFileId());
            });
        }
        Long total = productInfoMapper.findPageCount(dto);
        return PageResultFactory.createPageResult(productInfoAdminList, total, pageSize, pageNo);
    }

    @Override
    public List<ProductInfoVO> findList(ProductInfoDTO dto) {
        LambdaQueryWrapper<ProductInfoEntity> productInfoEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        productInfoEntityLambdaQueryWrapper.orderByDesc(ProductInfoEntity::getCreateTime);
        List<ProductInfoEntity> productInfoEntityList = super.list(productInfoEntityLambdaQueryWrapper);
        return ConvertUtil.turn(productInfoEntityList, ProductInfoVO.class);
    }

    @Override
    public ProductInfoVO findDetail(ProductInfoDTO dto) {
        Long id = dto.getId();
        LambdaQueryWrapper<ProductInfoEntity> productInfoEntityLambdaQueryWrapper = Wrappers.lambdaQuery();
        productInfoEntityLambdaQueryWrapper.eq(ProductInfoEntity::getId, id);
        ProductInfoEntity productInfoEntity = super.getOne(productInfoEntityLambdaQueryWrapper, Boolean.FALSE);
        ProductInfoVO productInfoVO = ConvertUtil.turn(productInfoEntity, ProductInfoVO.class);
        productInfoVO.setImageDetailFileUrl(env.getProperty("file-path") + productInfoVO.getImageDetailFileId());
        productInfoVO.setManualFileUrl(env.getProperty("file-path") + productInfoVO.getManualFileId());
        return productInfoVO;
    }

    @Override
    public boolean add(ProductInfoDTO dto) {
        ProductInfoEntity productInfoEntity = ConvertUtil.turn(dto, ProductInfoEntity.class);
        productInfoEntity.setUpdateTime(new Date());
        return super.save(productInfoEntity);
    }

    @Override
    public boolean edit(ProductInfoDTO dto) {
        ProductInfoEntity productInfoEntity = ConvertUtil.turn(dto, ProductInfoEntity.class);
        return super.updateById(productInfoEntity);
    }

    @Override
    public boolean delete(ProductInfoDTO dto) {
        return super.removeById(dto);
    }

    @Override
    public boolean batchDelete(List<Long> ids) {
        if (CollectionUtils.isNotEmpty(ids)) {
            return super.removeByIds(ids);
        }
        return true;
    }

}
